package com.ruoyi.project.mq;

import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.ruoyi.project.app.entity.SmsEntity;
import com.ruoyi.project.constant.MqConstants;

import com.ruoyi.project.util.LvHangHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;


/**
 * @author hry
 * mq监听队列,消费端
 */
@Component
@Slf4j
public class MqReciever {

    @RabbitListener(queues = MqConstants.TEST_QUEUE)
    public void directReciever(String msg, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws Exception {
        try {
            System.err.println("String:"+msg);
            Gson gson = new Gson();
            SmsEntity smsEntity = gson.fromJson(msg, SmsEntity.class);
            LvHangHttpUtil.sendMsg(smsEntity.getPhone(), smsEntity.getSmsTypeEnum(),smsEntity.getJsonObject());
            //手动确认
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                log.info("sms_queue消息重回队列======");
                channel.basicAck(deliveryTag, false);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}
